package cn.bdqn.t354.controller;

import cn.bdqn.t354.pojo.Right;
import cn.bdqn.t354.pojo.Role;
import cn.bdqn.t354.service.RightService;
import cn.bdqn.t354.service.RoleService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/role")
public class RoleApiController {
    @Resource
    RoleService roleService;
    @Resource
    RightService rightService;

    @RequestMapping("/findAllRoles")
    @ResponseBody
    public Object findAllRoles() {
        return roleService.findAllRoles();
    }

    /**
     * 列表展示
     *
     * @param roleName
     * @param pageIndex
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public Map<String,Object> list(String roleName,
                       @RequestParam(defaultValue = "1") Integer pageIndex,
                       Model model) {
        Map<String,Object> map=new HashMap<>();
        //设置每页显示条数
        int pageSize = 3;

        //查询每页显示的数据
        IPage<Role> rolePager = roleService.getRolesByPage(roleName, pageIndex,
                pageSize);
        map.put("rolePage", rolePager);

        return map;
    }

    /**
     * 跳转到新增页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/add")
    public Map<String,Object> add(Model model) {
        Map<String, Object> map = new HashMap<>();
        //查询所有的权限
        List<Right> rights = rightService.findAllRights();
        map.put("rights", rights);
        return map;
    }

    /**
     * 新增操作
     *
     * @param role
     * @param rightCodes
     * @return
     */
    @RequestMapping("/save")
    public Map<String,Object> save(@RequestBody Role role, String[] rightCodes) {
        Map<String, Object> map = new HashMap<>();
        try {
            roleService.addRole(role, rightCodes);
            map.put("result", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result", false);
        }
        return map;
    }

    /**
     * 跳转到修改页面
     *
     * @param roleId
     * @param model
     * @return
     */
    @RequestMapping("/edit")
    public Map<String,Object> edit(Long roleId, Model model) {
        Map<String, Object> map = new HashMap<>();
        //1.根据id查询角色信息
        Role role = roleService.getRole(roleId);
        map.put("role", role);
        return map;
    }

    /**
     * 修改操作
     *
     * @param role
     * @param rightCodes
     * @return
     */
    @RequestMapping("/update")
    public Map<String,Object> update(@RequestBody Role role, String[] rightCodes) {
        Map<String, Object> map = new HashMap<>();
        try {
            roleService.updateRole(role, rightCodes);
            map.put("result", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result", false);
        }
        return map;
    }

    /**
     * 删除操作
     *
     * @param roleId
     * @param model
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(Long roleId, Model model) {
        Map<String, Object> map = new HashMap<>();
        try {
            roleService.deleteRole(roleId);
            map.put("result", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result", false);
        }
        return map;
    }

}
